package com.example.demo1115.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo1115.dao.UserDao;
import com.example.demo1115.entity.Book;
import com.example.demo1115.entity.User;
import com.example.demo1115.service.UserService;
import com.example.demo1115.util.JwUtil;
import com.example.demo1115.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public Result findByusernameandpass(String username, String password) {

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        wrapper.eq("password",password);
        List<User> users = userDao.selectList(wrapper);
        System.out.println("users.toString() = " + users.toString());
        if (users.toString().equals("[]")|| users==null){
            return new Result(500,"登录失败");
        }else {
            Map<String,Object> map = new HashMap<>();
            map.put("username",username);
            map.put("id",users.get(0).getId());
            map.put("name",users.get(0).getName());

            String token = JwUtil.createToken(map);
            return new Result(200,"登录成功",token);
        }
    }
}
